<?php

require_once('persistence/AbstractGenericDao.class.php');
require_once('persistence/ICategoryDao.class.php');
require_once('entity/Category.class.php');

define('_SQL_FETCH_OFFERED_CATEGORIES', <<<EOD
SELECT
  oc.offered_category_id,
  c.category_id,
  c.title
FROM
  offered_category AS oc
JOIN
  category AS c ON c.category_id = oc.category_id
EOD
);

define('_SQL_FETCH_NEEDED_CATEGORIES', <<<EOD
SELECT
  nc.needed_category_id,
  c.category_id,
  c.title
FROM
  needed_category AS nc
JOIN
  category AS c ON c.category_id = nc.category_id
EOD
);

class GenericCategoryDao extends AbstractGenericDao implements ICategoryDao {

	public function GenericCategoryDao($db) {
		parent::__construct($db);
	}
	
	public function fetchOfferedCategories() {
		Log::instance()->debug("Invoking fetchOfferedCategories()");
		
		$categories = array();
		
		$rows = $this->dbQuery(_SQL_FETCH_OFFERED_CATEGORIES);
		foreach($rows as $row) {
			array_push($categories, new Category($row['category_id'], true, $row['offered_category_id'], $row['title']));
		}
		
		return $categories;
		
	}
	
	public function fetchNeededCategories() {
		Log::instance()->debug("Invoking fetchNeededCategories()");
		$categories = array();
		
		$rows = $this->dbQuery(_SQL_FETCH_NEEDED_CATEGORIES);
		foreach($rows as $row) {
			array_push($categories, new Category($row['category_id'], true, $row['needed_category_id'], $row['title']));
		}
		
		return $categories;
	}
	
}
?>